package com.hei.controller;

import com.alibaba.fastjson.JSONObject;
import com.hei.util.FileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class FileController {
    @RequestMapping("/uploadImage")
    @ResponseBody
    public String uploadImage(HttpServletRequest request,
                              HttpServletResponse response,
                              HttpSession session,
                              @RequestParam(value = "editormd-image-file") MultipartFile file
                              )  {
        JSONObject res = new JSONObject();
        //String IMAGE_PATH=request.getSession().getServletContext().getRealPath("/")+"\\static\\image\\";
        String IMAGE_PATH="D:\\tomcatImage";
        //String IMAGE_PATH="/image";
        try {
            String path = FileUpload.fileUpload(file, IMAGE_PATH);
            res.put("success",1);
            res.put("message","上传成功");
            res.put("url",request.getContextPath()+"/static/image/"+path);
        }catch (Exception e){
            System.out.println(e);
        }
        return res.toString();

    }

}
